# 爬取豆瓣电影数据
import json

import requests

if __name__ == "__main__":
    url = 'https://movie.douban.com/j/chart/top_list'

    # 重新封装参数
    param = {
        "type": "11",
        "interval_id": '100:90',
        "action": '',
        "start": 0,  # 从中第几部电影开始取
        "limit": 20  # 一次取出的条数是20条数据
    }

    heads = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36'
    }

    result = requests.get(url=url, params=param, headers=heads)

    print(result.request.url)  # 查看请求是由谁发起的
    print(result.request.headers)  # 查看请求头信息

    print(result.text)
    print('----------------------------------------')
    print(result.json())

    list_data = result.json()
    file = open(f'../爬取文件/04_豆瓣电影.json', mode='w', encoding='utf-8')
    json.dump(list_data, fp=file, ensure_ascii=False)

    result.close()
